using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Ultimate.DAL.CollectionClasses;
using Ultimate.DAL;
using Ultimate.DAL.HelperClasses;
using Ultimate.DAL.EntityClasses;
using Ultimate.DAL.FactoryClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;

using CommunityServer.Components;

namespace CommunityServerWeb.RegistrationSystem.Controls
{
    public partial class ScoreEntry : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadnBindData();
        }

        private void LoadnBindData()
        {
            grdGamesNeedEntry.DataSource = Ultimate.DAL.StoredProcedureCallerClasses.RetrievalProcedures.GetGamesThatNeedScores(CurrentPlayer.UserID);
            grdGamesNeedEntry.DataBind();
        }

        protected void grdGamesNeedEntry_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //grdGamesNeedEntry.SelectedIndex = e.commans
            //throw new Exception("The method or operation is not implemented.");
        }


        private PlayerEntity _pe;
        private PlayerEntity CurrentPlayer
        {
            get
            {
                if (_pe == null)
                {
                    _pe = new PlayerEntity();
                    System.Web.Security.MembershipUser mu = System.Web.Security.Membership.GetUser();
                    _pe.FetchUsingUCUserID((Guid)mu.ProviderUserKey);
                }

                return _pe;
            }
        }

        /// <summary>
        /// Enter the scores!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int id = (int)grdGamesNeedEntry.SelectedDataKey.Value;
            object cnt = grdGamesNeedEntry.SelectedRow.FindControl("playerTeamId");
            int playerTeamId = -1;
            if (cnt != null && cnt is Label)
            {
                playerTeamId = int.Parse(((Label)cnt).Text);
            }

            SubmitScores(id, playerTeamId);

            pnlScoreEntry.Visible = false;

            ClearScoreEntry();

            LoadnBindData();

        }

        /// <summary>
        /// Clears the Score entry fields.
        /// </summary>
        private void ClearScoreEntry()
        {
            homeScore.SelectedIndex = -1;
            awayScore.SelectedIndex = -1;
            alWarmby.Items.Clear();
            teamSpirit.SelectedIndex = -1;
        }

        /// <summary>
        /// Submits the scores teh user has entered
        /// </summary>
        /// <param name="id"></param>
        /// <param name="playerTeamId"></param>
        private void SubmitScores(int id, int playerTeamId)
        {
            GameEntity game = new GameEntity(id);

            if (game.HomeTeamID == playerTeamId)
            {
                game.HomeHomeScore = int.Parse(homeScore.SelectedValue);
                game.HomeAwayScore = int.Parse(awayScore.SelectedValue);
                game.HomeSpiritScore = double.Parse(teamSpirit.SelectedValue);

                game.HomeWharmbyPlayerID = int.Parse(alWarmby.SelectedValue);
            }
            else
            {
                game.AwayAwayScore = int.Parse(homeScore.SelectedValue);
                game.AwayHomeScore = int.Parse(awayScore.SelectedValue);
                game.AwaySpiritScore = double.Parse(teamSpirit.SelectedValue);

                game.AwayWharmbyPlayerID = int.Parse(alWarmby.SelectedValue);
            }

            game.Save();
        }

        protected void grdGamesNeedEntry_SelectedIndexChanged(object sender, EventArgs e)
        {
            pnlScoreEntry.Visible = true;

            int id = (int)grdGamesNeedEntry.SelectedDataKey.Value;
            object cnt = grdGamesNeedEntry.SelectedRow.FindControl("playerTeamId");
            int playerTeamId = -1;
            if (cnt != null && cnt is Label)
            {
                playerTeamId = int.Parse(((Label)cnt).Text);
            }

            LoadGameForEntry(id, playerTeamId);
        }

        private void LoadGameForEntry(int id, int playerTeamId)
        {
            GameEntity game = new GameEntity(id);

            gameLabel.Text = game.Team_.TeamName + " vs " + game.Team.TeamName + " on " + game.GameDay.ToString("MMM dd yy");

            int opponentId = -1;
            if (game.HomeTeamID == playerTeamId)
            {
                opponentId = game.AwayTeamID;
            }
            else
            {
                opponentId = game.HomeTeamID;
            }

            PlayerCollection opponents = new PlayerCollection();
            IPredicateExpression filter = new PredicateExpression();
            filter.Add(PredicateFactory.CompareValue(
                TeamPlayerFieldIndex.TeamID,
                ComparisonOperator.Equal,
                opponentId));

            IRelationCollection relations = new RelationCollection();
            relations.Add(PlayerEntity.Relations.TeamPlayerEntityUsingUserID);

            opponents.GetMulti(filter, relations);

            alWarmby.DataTextField = "LastName";
            alWarmby.DataValueField = "PlayerID";
            alWarmby.DataSource = opponents;
            alWarmby.DataBind();
        }


    }
}